package com.checking.attendance.controller;

import com.checking.attendance.pojo.Course;

import com.checking.attendance.service.CourseService;
import com.checking.attendance.util.DateUtil;
import com.checking.attendance.util.ReturnUtil;
import com.checking.attendance.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class CourseJsonController {
    @Autowired
    private CourseService courseService;

    //添加一条课程信息
    @ResponseBody
    @RequestMapping(value = "/member/courseInsert",method = RequestMethod.POST)
    public ModelMap courseInsert(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getTeacherId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            course.setId(UuidUtil.runVerifyCode(9));
            course.setCreateTime(DateUtil.getPresentDate());

            int count = courseService.insertSelective(course);
            if (count == 0){
                return ReturnUtil.error("添加课程信息失败");
            }

            return ReturnUtil.success("添加课程信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //根据当前用户id查询所有课程记录
    @ResponseBody
    @RequestMapping(value = "/member/queryCourseInfoAll",method = RequestMethod.POST)
    public ModelMap queryCourseInfoAll(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getTeacherId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            List<Course> courseList = courseService.queryCourseInfoAll(course.getTeacherId());
            if (courseList.size() == 0){
                return ReturnUtil.error("暂无课程信息");
            }

            return ReturnUtil.success("获取课程信息成功", courseList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据当期学生ID和选课状态查询多条记录
    @ResponseBody
    @RequestMapping(value = "/member/queryStudentCourseInfoAll",method = RequestMethod.POST)
    public ModelMap queryStudentCourseInfoAll(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getStudentId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(course.getSelectedState())){
                return ReturnUtil.error("选课状态不能为空", null, null);
            }

            List<Course> courseList = courseService.queryStudentCourseInfoAll(course);
            if (courseList.size() == 0){
                return ReturnUtil.error("暂无课程信息");
            }

            return ReturnUtil.success("获取课程信息成功", courseList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //查询所有课程信息
    @ResponseBody
    @RequestMapping(value = "/member/allCourseInfo",method = RequestMethod.POST)
    public ModelMap allCourseInfo(@Validated @RequestBody Course course, BindingResult result){
        try {

            List<Course> courseList = courseService.selectByPrimaryCourseAll();
            if (courseList.size() == 0){
                return ReturnUtil.error("暂无课程信息");
            }

            return ReturnUtil.success("获取课程信息成功", courseList);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //删除一条课程信息
    @ResponseBody
    @RequestMapping(value = "/member/deletCourse",method = RequestMethod.POST)
    public ModelMap deletCourse(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            int count = courseService.deleteByPrimaryKey(course.getId());
            if (count == 0){
                return ReturnUtil.error("删除课程信息失败");
            }

            return ReturnUtil.success("删除课程信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //查询一条课程记录
    @ResponseBody
    @RequestMapping(value = "/member/queryCourseInfo",method = RequestMethod.POST)
    public ModelMap queryCourseInfo(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            Course courses = courseService.selectByPrimaryKey(course.getId());
            if (courses == null){
                return ReturnUtil.error("暂无课程信息");
            }

            return ReturnUtil.success("获取课程信息成功", courses);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //更新一条课程信息
    @ResponseBody
    @RequestMapping(value = "/member/updateCourse",method = RequestMethod.POST)
    public ModelMap updateCourse(@Validated @RequestBody Course course, BindingResult result){
        try {
            if(StringUtils.isEmpty(course.getId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            course.setUpdateTime(DateUtil.getPresentDate());

            int count = courseService.updateByPrimaryKeySelective(course);
            if (count == 0){
                return ReturnUtil.error("更新课程信息失败");
            }

            return ReturnUtil.success("更新课程信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

}
